﻿# MetadataInfoPanel.py
# 
# A simple Comicrack script to display the selected comic metadata in the Infopanel
# 
# author: perezmu
# version: 0.5
#
# Changelog:
#	v0.6 - Added Fileformat
#	v0.5 - Cleaned up html using css
#		 - Changed 'Notes' field placement
#		 - Included newline in textual fields
#   v0.4 - Corrected paths
#	v0.3 - Removed all references to os module
#   v0.2 - Added a local copy of os module
##########################################################################

import clr
import sys

from cYo.Projects.ComicRack.Engine.Display import *
from System.Collections.Generic import *
from System import String

clr.AddReference('System.Drawing')
clr.AddReferenceByPartialName("System.Windows.Forms")
from System.Windows.Forms import *

import System

# A simple script to display the selected comic metadata in the Infopanel
#
#@Name Metadata Info Panel
#@Hook ComicInfoHtml
#@Enabled true
#@Description A simple script to display the selected comic metadata in the Infopanel
#@Image logo_small.png

def MetadataInfoPanel(books):
    
	#Only get data for the first book selected
	if not books:
		return "Select any book"
		
	b = books[0]
	
	#file directory
	script_dir =  __file__[:-len('MetadataInfoPanel.py')] 
	html_template = script_dir + "ComicInfoPanel.html"
	rating_star = script_dir + "rating_star_full.png"
	com_rating_star = script_dir + "com_rating_star_full.png"
	cover = script_dir + "temp_cover.jpg"
	
		
	f = open(html_template,"r")
	html_temp = f.readlines()
	html = ""
	for line in html_temp:
		html = html + line

	# Cover Image - code from Ricardo Pescuma Domenecci's Series Info Panel Script
	coverIndex = 0 
	if b.FrontCoverPageIndex > 0:
		coverIndex = b.FrontCoverPageIndex
	cover_image = ComicRack.App.GetComicPage(b, coverIndex)
	cover_image.Save(cover, System.Drawing.Imaging.ImageFormat.Jpeg)
	
	html = String.Replace(html,"$temp_cover.jpg", cover)
			
		
	html = String.Replace(html, "$Series", b.Series)
	html = String.Replace(html, "$Volume", str(b.Volume))
	html = String.Replace(html, "$Number", str(b.Number))
	html = String.Replace(html, "$Title", b.Title)
	html = String.Replace(html, "$Summary", String.Replace(b.Summary, "\n", "<p>"))
	html = String.Replace(html, "$Publisher", b.Publisher)
	if b.Imprint:
		html = String.Replace(html, "($Imprint)", "("+b.Imprint+")")
	else:
		html = String.Replace(html, "($Imprint)", "")
	if b.Year <> -1 and b.Month <> -1:
		html = String.Replace(html, "$Month/$Year", str(b.Month)+"/"+str(b.Year))
	elif b.Year <> -1:
		html = String.Replace(html, "$Month/$Year", str(b.Year))		
	else:
		html = String.Replace(html, "$Month/$Year", "")
	if b.Format:
		html = String.Replace(html, "$Format", b.Format)
	else:
		html = String.Replace(html, "$Format", "")
	
	html = String.Replace(html, "$FileFormat", b.FileFormat)
	
	html = String.Replace(html, "$Writer", b.Writer)
	html = String.Replace(html, "$Inker", b.Inker)
	html = String.Replace(html, "$Penciller", b.Penciller)
	html = String.Replace(html, "$Colorist", b.Colorist)
	html = String.Replace(html, "$Editor", b.Editor)
	html = String.Replace(html, "$Letterer", b.Letterer)
	html = String.Replace(html, "$CoverArtist", b.CoverArtist)
	
	if b.Tags:
		html = String.Replace(html, "$Tags", b.Tags)
	else:
		html = String.Replace(html, "$Tags", "")
	if b.Notes:
		html = String.Replace(html, "$Notes", String.Replace(b.Notes, "\n", "<p>"))
	else:
		html = String.Replace(html, "$Notes", "")
	if b.Genre:
		html = String.Replace(html, "$Genre", b.Genre)
	else:
		html = String.Replace(html, "$Genre", "")
	if b.Characters:
		html = String.Replace(html, "$Characters", b.Characters)
	else:
		html = String.Replace(html, "$Characters", "")
	if b.Genre:
		html = String.Replace(html, "$Locations", b.Locations)
	else:
		html = String.Replace(html, "$Locations", "")
			
	if b.Teams:
		html = String.Replace(html, "$Teams", b.Teams)
	else:
		html = String.Replace(html, "$Teams", "")
	if b.Web:
		html = String.Replace(html, "$Web", "<a href="+b.Web+">Web page</a>")
	else:
		html = String.Replace(html, "<a>$Web</a>", "")
	
		
	if b.AlternateSeries <> "" and str(b.AlternateNumber) <> ""  and str(b.AlternateCount)<>"":
		html = String.Replace(html, "($AlternateSeries #$AlternateCount)", "("+b.AlternateSeries+" #"+str(b.AlternateNumber) + " of " + str(b.AlternateCount)+ ")")
		print str(b.AlternateNumber)
		print b.AlternateCount
	elif b.AlternateSeries <> "" and str(b.AlternateNumber) <> "":
		html = String.Replace(html, "($AlternateSeries #$AlternateCount)", "("+b.AlternateSeries+" #"+str(b.AlternateNumber) +")")
	elif b.AlternateSeries <> "": 		
		html = String.Replace(html, "($AlternateSeries #$AlternateCount)", "("+b.AlternateSeries+")")
	else:
		html = String.Replace(html, "($AlternateSeries #$AlternateCount)", "")
		
	
	rating = ""
	rating_i = 1
	if b.Rating > 1:
		for i in range(b.Rating):
			rating = rating + '<img src="'+rating_star+'" style="width: 24px; height: 24px;" alt="">'
			#rating_i = rating_i + 1
		html = String.Replace(html, "$Rating", rating)
	else:
		html = String.Replace(html, "$Rating", "")
	rating = ""
	if b.CommunityRating > 1:
		for i in range(b.CommunityRating):
			rating = rating + '<img src="'+com_rating_star+'" style="width: 24px; height: 24px;" alt="">'
			#rating_i = rating_i + 1
		html = String.Replace(html, "$ComRating", rating)
	else:
		html = String.Replace(html, "$ComRating", "")
	
	return html
	#return "!file:///C:/Documents%20and%20Settings/BlackCrystal™/Datos%20de%20programa/cYo/ComicRack/Scripts/Meta%20Data%20Info%20Panel/ComicInfoPanel.html"

